import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreAgreementService extends BaseService<any> {
  private readonly logger = new Logger(CoreAgreementService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getAgreement
   * 对应 PHP: CoreAgreementService_core::getAgreement()
   * 逻辑类型: undefined - undefined
   */
  async getAgreement(site_id: number, key: number) {
    // 基于PHP真实逻辑: getAgreement
    // PHP原文: if(!array_key_exists($key, AgreementDict::getType())) throw new CommonException('AGREEMENT_TYPE_NOT_EXIST');         $where = array(             ['agr...
if(!array_key_exists(key, AgreementDict.getType())) throw new BusinessException('AGREEMENT_TYPE_NOT_EXIST'];
        const where = [
            ['agreement_key', '=', key),
            ['site_id', '=', site_id)
        ];
        const info = this.model.where(where).field('site_id, agreement_key, title, content, create_time, update_time').append(.agreement_key_name).findOrEmpty().toArray(];
        if(!info))
        {
            const info = [
                site_id: site_id,
                agreement_key: key,
                agreement_key_name: AgreementDict.getType()[key] || '',
                title: '',
                content: '',
                create_time: '',
                update_time: ''
            ];
  }

  /**
   * findRecord (原方法名: find)
   * 对应 PHP: CoreAgreementService_core::find()
   * 逻辑类型: undefined - undefined
   * 注意: 为避免与BaseService方法冲突，已重命名
   */
  async findRecord(site_id: number, key: number) {
    // 基于PHP真实逻辑: find
    // PHP原文: if(!array_key_exists($key, AgreementDict::getType())) throw new CommonException('AGREEMENT_TYPE_NOT_EXIST');         $where = array(             ['agr...
if(!array_key_exists(key, AgreementDict.getType())) throw new BusinessException('AGREEMENT_TYPE_NOT_EXIST'];
        const where = [
            ['agreement_key', '=', key),
            ['site_id', '=', site_id)
        ];
        return this.model.where(where).findOrEmpty(];
    }
    /**
     * 设置协议
     * @param int site_id
     * @param string key
     * @param string title
     * @param string content
     * @return bool
     */
    async setAgreement(int site_id, string key, string title, string content)
    {
        if(!array_key_exists(key, AgreementDict.getType())) throw new BusinessException('AGREEMENT_TYPE_NOT_EXIST'];
        const where = [
            ['agreement_key', '=', key),
            ['site_id', '=', site_id)
        ];
        const data = [
            site_id: site_id,
            agreement_key: key,
            title: title,
            content: content
        ];
        const agreement = this.find(site_id, key);
        if(agreement.isEmpty()){
            data.create_time = time(];
            const res = this.model.create(data);
  }

  /**
   * setAgreement
   * 对应 PHP: CoreAgreementService_core::setAgreement()
   * 逻辑类型: undefined - undefined
   */
  async setAgreement(site_id: number, key: number, title: number, content: number) {
    // 基于PHP真实逻辑: setAgreement
    // PHP原文: if(!array_key_exists($key, AgreementDict::getType())) throw new CommonException('AGREEMENT_TYPE_NOT_EXIST');         $where = array(             ['agr...
if(!array_key_exists(key, AgreementDict.getType())) throw new BusinessException('AGREEMENT_TYPE_NOT_EXIST'];
        const where = [
            ['agreement_key', '=', key),
            ['site_id', '=', site_id)
        ];
        const data = [
            site_id: site_id,
            agreement_key: key,
            title: title,
            content: content
        ];
        const agreement = this.find(site_id, key);
        if(agreement.isEmpty()){
            data.create_time = time(];
            const res = this.model.create(data);
  }
}
